/**
 * @Author：ming
 * @Date：2023/10/30 5:15 PM
 * @Filename：Hot21
 */

/**
 * @author ming
 */
public class Hot21 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }

        if (list2 == null) {
            return list1;
        }

        ListNode p = list1;
        ListNode q = list2;

        ListNode newHead = new ListNode(0);
        ListNode head = newHead;
        while (q != null || p != null) {
            if(q==null){
                head.next =p;
                break;
            }else if(p==null){
                head.next=q;
                break;
            }else {
                //比较大小
                if(q.val<=p.val){
                    head.next = q;
                    q=q.next;
                    head =head.next;
                }else {
                    head.next=p;
                    p=p.next;
                    head=head.next;
                }
            }
        }

        return newHead.next;
    }
}
